/*
 * Copyright 2017 Baiyyy
 * @(#)DeviceCmdController.java
 */

package com.byyy.ccts.modules.cc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.byyy.ccts.core.common.controller.BaseCRUDController;
import com.byyy.ccts.core.model.PageJson;
import com.byyy.ccts.core.query.data.PropertyPreFilterable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.utils.QueryableConvertUtils;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.cc.entity.ccts.TSDeviceOptCmd;
import com.byyy.ccts.modules.cc.entity.ccts.TSHumiture;
import com.byyy.ccts.modules.cc.service.IDeviceOptCmdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 设备命令执行
 * Created by DELL on 2018-5-16.
 */
@Controller
@RequestMapping("${admin.url.prefix}/cc/deviceOptCmd")
@RequiresPathPermission("cc:deviceOptCmd")
public class DeviceOptCmdController extends BaseCRUDController<TSDeviceOptCmd, String> {

    @Autowired
    private IDeviceOptCmdService deviceOptCmdService;

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "deviceOptCmdAjaxList", method = { RequestMethod.GET, RequestMethod.POST })
    public void deviceOptCmdAjaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                                 HttpServletResponse response) throws IOException {
        EntityWrapper<TSDeviceOptCmd> entityWrapper = new EntityWrapper<TSDeviceOptCmd>(TSDeviceOptCmd.class);
        if (!StringUtils.isEmpty(request.getParameter("deviceId"))) {
            entityWrapper.eq("A.DEVICE_ID", request.getParameter("deviceId"));
        }
        if (!StringUtils.isEmpty(request.getParameter("cangku"))) {
            entityWrapper.eq("B.CANGKU", request.getParameter("cangku"));
        }
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, TSHumiture.class);
        SerializeFilter filter = propertyPreFilterable.constructFilter(TSHumiture.class);
        PageJson<TSDeviceOptCmd> pagejson = new PageJson<TSDeviceOptCmd>(this.deviceOptCmdService.deviceOptCmdList(queryable, entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }
}
